package hhm.android.base.application;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import androidx.lifecycle.ProcessLifecycleOwner;

import com.alibaba.android.arouter.launcher.ARouter;

import hhm.android.library.netWorkState.NetWorkDialog;
import hhm.android.library.netWorkState.NetworkStateListener;
import hhm.android.library.netWorkState.NetworkStateUtils;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

public abstract class BaseApplication extends Application implements NetWorkStateListener {

    private NetworkStateUtils networkStateUtils;
    private final int NETWORK_ALERT_TIP = 0;
    private final int NETWORK_TOAST_TIP = 1;
    private final int NETWORK_NO_TIP = 2;
    private int networkTip = NETWORK_NO_TIP;
    public static Context applicationContext;

    @Override
    public void onCreate() {
        super.onCreate();

        applicationContext = this;

        //ARouter初始化
        ARouter.init(this);

        ProcessLifecycleOwner.get().getLifecycle().addObserver(new BaseApplicationLifeCycle());

        networkStateUtils = NetworkStateUtils.Companion.getInstance(this, new NetworkStateListener() {
            @Override
            public void connect() {
                switch (networkTip) {
                    case NETWORK_ALERT_TIP:
//                        if (canShowDialog()) {
                            Intent i = new Intent(BaseApplication.this, NetWorkDialog.class);
                            i.putExtra("type", 1);
                            i.setFlags(FLAG_ACTIVITY_NEW_TASK);
                            startActivity(i);
//                        }
                        break;
                    case NETWORK_TOAST_TIP:
                        Toast.makeText(BaseApplication.this, "网络状态已连接", Toast.LENGTH_SHORT).show();
                        break;
                    default:

                        break;
                }
                netWorkState(true);
            }

            @Override
            public void unConnect() {
                switch (networkTip) {
                    case NETWORK_ALERT_TIP:
                        if (canShowDialog()) {
                            Intent i = new Intent(BaseApplication.this, NetWorkDialog.class);
                            i.putExtra("type", 0);
                            i.setFlags(FLAG_ACTIVITY_NEW_TASK);
                            startActivity(i);
                        }
                        break;
                    case NETWORK_TOAST_TIP:
                        Toast.makeText(BaseApplication.this, "网络状态已断开", Toast.LENGTH_SHORT).show();
                        break;
                    default:

                        break;
                }
                netWorkState(false);
            }
        });
    }

    protected abstract boolean canShowDialog();

    public void setNetworkAlertTip() {
        networkTip = NETWORK_ALERT_TIP;
    }

    public void setNetworkToastTip() {
        networkTip = NETWORK_TOAST_TIP;
    }

    public void setNetworkNoTip() {
        networkTip = NETWORK_NO_TIP;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        ARouter.getInstance().destroy();
        networkStateUtils.destory();
    }
}
